package com.dtstack.rocketmq.facade.assembler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dtstack.rocketmq.domain.response.shopify.Edge;
import com.dtstack.rocketmq.domain.response.shopify.OrderResponse;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * OrderResposeAssembler
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/10/24
 */
public class OrderResponseAssembler {


    public static void main(String[] args) {
        try {
            String jsonString = "{\n" +
                    "  \"data\": {\n" +
                    "    \"orders\": {\n" +
                    "      \"edges\": [\n" +
                    "        {\n" +
                    "          \"node\": {\n" +
                    "            \"id\": \"gid://shopify/Order/5991704363249\",\n" +
                    "            \"name\": \"#FG56976\",\n" +
                    "            \"totalPrice\": \"1231.12\",\n" +
                    "            \"createdAt\": \"2024-10-19T03:16:40Z\",\n" +
                    "            \"customer\": {\n" +
                    "              \"firstName\": \"Anthony\",\n" +
                    "              \"lastName\": \"Sales\",\n" +
                    "              \"email\": \"patricioqueimado@yahoo.com\",\n" +
                    "              \"phone\": \"None\"\n" +
                    "            },\n" +
                    "            \"shippingAddress\": {\n" +
                    "              \"address1\": \"12052 Crystal Ct\",\n" +
                    "              \"address2\": \"None\",\n" +
                    "              \"city\": \"Chino\",\n" +
                    "              \"province\": \"California\",\n" +
                    "              \"country\": \"United States\",\n" +
                    "              \"zip\": \"91710\"\n" +
                    "            },\n" +
                    "            \"discountCode\": \"FGEB12\",\n" +
                    "            \"discountCodes\": [\n" +
                    "              \"FGEB12\"\n" +
                    "            ],\n" +
                    "            \"discountApplications\": {\n" +
                    "              \"edges\": [\n" +
                    "                {\n" +
                    "                  \"node\": {\n" +
                    "                    \"allocationMethod\": \"EACH\",\n" +
                    "                    \"index\": 0,\n" +
                    "                    \"targetSelection\": \"ENTITLED\",\n" +
                    "                    \"targetType\": \"LINE_ITEM\",\n" +
                    "                    \"value\": {\n" +
                    "                      \"percentage\": 12.0\n" +
                    "                    }\n" +
                    "                  }\n" +
                    "                }\n" +
                    "              ]\n" +
                    "            },\n" +
                    "            \"lineItems\": {\n" +
                    "              \"edges\": [\n" +
                    "                {\n" +
                    "                  \"node\": {\n" +
                    "                    \"name\": \"Lab Alexandrite Leafy Inspired Engagement Ring Sets 2pcs - 14K White Gold / Lab Alexandrite\",\n" +
                    "                    \"quantity\": 1,\n" +
                    "                    \"sku\": \"DZR23157-2\",\n" +
                    "                    \"isGiftCard\": false,\n" +
                    "                    \"product\": {\n" +
                    "                      \"id\": \"gid://shopify/Product/7993088475377\",\n" +
                    "                      \"productType\": \"\",\n" +
                    "                      \"createdAt\": \"2023-09-08T08:44:20Z\",\n" +
                    "                      \"category\": {\n" +
                    "                        \"fullName\": \"Uncategorized\",\n" +
                    "                        \"name\": \"Uncategorized\"\n" +
                    "                      }\n" +
                    "                    }\n" +
                    "                  }\n" +
                    "                }\n" +
                    "              ]\n" +
                    "            },\n" +
                    "            \"presentmentCurrencyCode\": \"USD\",\n" +
                    "            \"transactions\": [\n" +
                    "              {\n" +
                    "                \"authorizationCode\": \"None\",\n" +
                    "                \"gateway\": \"Oceanpayment(Direct)\",\n" +
                    "                \"id\": \"gid://shopify/OrderTransaction/6981105418481\",\n" +
                    "                \"manuallyCapturable\": false,\n" +
                    "                \"order\": {\n" +
                    "                  \"discountCode\": \"FGEB12\"\n" +
                    "                }\n" +
                    "              }\n" +
                    "            ]\n" +
                    "          }\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    }\n" +
                    "  },\n" +
                    "  \"extensions\": {\n" +
                    "    \"cost\": {\n" +
                    "      \"requestedQueryCost\": 27,\n" +
                    "      \"actualQueryCost\": 15,\n" +
                    "      \"throttleStatus\": {\n" +
                    "        \"maximumAvailable\": 4000.0,\n" +
                    "        \"currentlyAvailable\": 3985,\n" +
                    "        \"restoreRate\": 175.0\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}";

//            JSONObject object = JSON.parseObject(jsonString);
//            System.out.println(object);
            OrderResponse orderData = JSON.parseObject(jsonString, OrderResponse.class);
            List<Edge> edges = orderData.getData().getOrders().getEdges();
            System.out.println(orderData.getData().getOrders());
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    //展示JSON列表数据
    public static Map<String, Object> showJson(String jsonData) {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = JSON.parseObject(jsonData);
        //1. 获取所有的Key,得到key对应的value
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            //2.1 如果value为JSONArray类型，则解析
            if (value instanceof JSONArray) {
                JSONArray jsonArray = new JSONArray(new LinkedList<>());
                JSONArray array = jsonObject.getJSONArray(key);
                for (int i = 0; i < array.size(); i++) {
                    Object object = array.get(i);
                    //如果value里面是List: ["aaa","bbb","ccc"]; 或者是value里面是JSONObject[{},{},{}]
                    if (object instanceof String) {
                        map.put(key, array);
                        //jsonArray.add(array);
                    } else {
                        Map<String, Object> stringObjectMap = showJson(String.valueOf(object));
                        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
                            map.put(entry.getKey(), entry.getValue());
                        }
                        jsonArray.add(stringObjectMap);
                        map.put(key, jsonArray);
                    }
                }
                //2.2 如果value为JSONObject类型，则解析
            } else if (value instanceof JSONObject) {
                Map<String, Object> stringObjectMap = showJson(String.valueOf(value));
                map.put(key, stringObjectMap);

                //2.3 如果value为JSON类型，则装入map
            } else {
                map.put(key, value);
            }
        }
        return new JSONObject(map);
    }

}
